package com.sgcc.pda.lib_tools_database.factory;

import com.sgcc.pda.lib_tools_database.DBMaster;
import com.sgcc.pda.lib_tools_database.comment.CommonDaoUtils;
import com.sgcc.pda.lib_tools_database.gen.DaoSession;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;

import java.util.List;

/**
 * 描 述：通用增、删、改、查接口抽象实现
 * 作 者：小卷毛  2020-09-13 10:12
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
public abstract class Abstractable<T, K extends AbstractDao> implements ICommonable<T> {

    protected CommonDaoUtils<T> mCommonDaoUtils;

    @SuppressWarnings({"unchecked", "rawtypes"})
    public Abstractable() {
        K db = getDao(DBMaster.getSingleton().getDBEncryption());
        mCommonDaoUtils = new CommonDaoUtils(db);
    }

    @Override
    public void insert(T bean) {
        if (bean != null) mCommonDaoUtils.insert(bean);
    }

    @Override
    public void insertList(List<T> list) {
        if (list != null && list.size() > 0) {
            mCommonDaoUtils.insert(list);
        }
    }

    @Override
    public void delete(T bean) {
        mCommonDaoUtils.delete(bean);
    }

    @Override
    public void deleteAll() {
        mCommonDaoUtils.deleteAll();
    }

    @Override
    public T findData(Property where, String param) {
        return mCommonDaoUtils.query(where, param);
    }

    @Override
    public List<T> findDataAll() {
        return mCommonDaoUtils.queryAll();
    }

    @Override
    public List<T> findByParam(Property[] wheres, String[] params) {
        return mCommonDaoUtils.query(wheres, params);
    }

    @Override
    public void updateData(T bean) {
        mCommonDaoUtils.update(bean);
    }

    @Override
    public void updateList(List<T> list) {
        mCommonDaoUtils.update(list);
    }

    /**
     * 获取对应的数据访问DAO
     */
    public abstract K getDao(DaoSession session);

}
